import { Injectable } from '@angular/core';
import { FormControl, Validators, FormGroup, FormBuilder } from '@angular/forms';
import { ConfigMetaBase } from '../utils/config-meta-base';

@Injectable({
  providedIn: 'root'
})
export class ConfigControlService {
  constructor(private fb: FormBuilder) { }

  toFormGroup(metas: ConfigMetaBase<any>[] ) {
    let group: any = {};
    if (metas) {
      metas.forEach(meta => {
        group[meta.key] = meta.required ? new FormControl(meta.value, Validators.required)
                                        : new FormControl(meta.value);
      });
    }
    return this.fb.group(group);
  }
}
